package com.gdut.simple.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gdut.simple.R;
import com.gdut.simple.data.remote.api.GankApi;
import com.gdut.simple.view.adapter.SimplePageAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomepageFragment extends Fragment {

    @BindView(R.id.id_tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.id_wrap_view_pager)
    ViewPager mPager;

    public static HomepageFragment newInstance() {
        return new HomepageFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_homepage, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final List<String> tabs = Arrays.asList(GankApi.NAMES);
        final List<Fragment> fragments = new ArrayList<>();
        for (String type : GankApi.TYPES) {
            fragments.add(GankFragment.newInstance(type));
        }
        final SimplePageAdapter mPagerAdapter = new SimplePageAdapter(getChildFragmentManager(), fragments, tabs);
        mPager.setOffscreenPageLimit(tabs.size() - 1);
        mPager.setAdapter(mPagerAdapter);
        mTabLayout.setupWithViewPager(mPager);
    }

}
